﻿
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace Refraction
{
    /// <summary>
    /// Initialize a new instance of a QuadRenderComponent.
    /// Is a DrawableGameComponent.
    /// Is used to draw a quad. Simular to the spritebatch class
    /// </summary>
    public partial class QuadRenderComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        #region Private Members
        VertexPositionTexture[] verts = null;
        short[] ib = null;

        #endregion


        #region Constructor
        /// <summary>
        /// Initialize a new instance of this drawableGameComponent.
        /// Can be used to draw a scene.
        /// </summary>
        public QuadRenderComponent(Game game)
            : base(game)
        {
        }
        #endregion


        #region LoadGraphicsContent

        protected override void LoadContent()
        {
            IGraphicsDeviceService graphicsService =
                (IGraphicsDeviceService)base.Game.Services.GetService(
                                            typeof(IGraphicsDeviceService));


            verts = new VertexPositionTexture[]
                        {
                            new VertexPositionTexture(
                                new Vector3(0,0,0),
                                new Vector2(1,1)),
                            new VertexPositionTexture(
                                new Vector3(0,0,0),
                                new Vector2(0,1)),
                            new VertexPositionTexture(
                                new Vector3(0,0,0),
                                new Vector2(0,0)),
                            new VertexPositionTexture(
                                new Vector3(0,0,0),
                                new Vector2(1,0))
                        };

            ib = new short[] { 0, 1, 2, 2, 3, 0 };

        }
        #endregion


        #region void Render(Vector2 v1, Vector2 v2)
        /// <summary>
        /// Draws the scene.
        /// </summary>
        /// <param name="v1">Left upper corner</param>
        /// <param name="v2">Right lower corner</param>
        public void Render(Vector2 v1, Vector2 v2)
        {
            IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)
                Game.Services.GetService(typeof(IGraphicsDeviceService));

            GraphicsDevice device = graphicsService.GraphicsDevice;
            device.BlendState = BlendState.Opaque;

            verts[0].Position.X = v2.X;
            verts[0].Position.Y = v1.Y;

            verts[1].Position.X = v1.X;
            verts[1].Position.Y = v1.Y;

            verts[2].Position.X = v1.X;
            verts[2].Position.Y = v2.Y;

            verts[3].Position.X = v2.X;
            verts[3].Position.Y = v2.Y;

            device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, verts, 0, 4, ib, 0, 2);
        }
        #endregion
    }
}
